﻿using UnityEngine;
using System.Collections.Generic;

public static class CombinableItemExtensions
{
    public static void AddForceAtLocalPosition(this CombinableItem item,Vector3 pos,float strength,float localAngle)
    {
        //获取刚体
        var _rigid =item.GetComponentInParent<Rigidbody2D>();
        if (_rigid != null)
        {
            //添加力
            _rigid.AddForceAtPosition(item.GetWorldDirection(localAngle) * strength, item.transform.TransformPoint(pos));

        }
 //       Debug.Log(item.GetWorldDirection(Angle));
    }
    public static void AddForceAtLocalPosition(this CombinableItem item, Vector3 pos, Vector3 strength)
    {
        var _rigid = item.GetComponentInParent<Rigidbody2D>();
        if (_rigid != null)
        {
            _rigid.AddForceAtPosition( strength, item.transform.TransformPoint(pos));

        }
        //       Debug.Log(item.GetWorldDirection(Angle));
    }
    public static List<CombinableItem> GetOtherDisconnectedCombinableItemInAllColliders(this CombinableItem item)
    {
        List<CombinableItem> list=new List<CombinableItem>();
        List<Collider2D> colliders =new List<Collider2D>(item.GetComponentsInChildren<Collider2D>());
        foreach(var x in colliders)
        {
            var tp = x.GetInColliderParent<CombinableItem>(LayerMask.NameToLayer("Player"));
            foreach(var co in tp)
            {
                if(co!=item&&GameCombine.CombineManager.Get<GameCombine.IItemCombineManager>().CheckIfConnected(co.ItemFieldSetting.ItemName,item.ItemFieldSetting.ItemName)==false)
                list.UniqueAdd<CombinableItem>(co);
            }
        }
        return list;
    }
   public static void RotateRelativeToPointInWorld(this CombinableItem item,Vector3 pointPos,float angleSpeedInDegree)
    {
        float angle = angleSpeedInDegree * Time.fixedDeltaTime/180*Mathf.PI;
        Vector3 itemPos = item.transform.position;
        Vector3 tp=UnsafeTool.TurnAngle(itemPos-pointPos, angle);
        pointPos.z = item.transform.position.z;
        item.transform.position = pointPos + tp;
        item.RotationInDegree += angle / Mathf.PI * 180;
    }
    public static bool CheckIfOverlapInCombine(this CombinableItem item)
    {
        var items = item.transform.parent.GetComponentsInChildren<CombinableItem>();
        foreach(var x in items)
        {
            var list = x.GetOtherDisconnectedCombinableItemInAllColliders();
            list.RemoveAll(s => s.ItemFieldSetting.ItemName == x.transform.name);
            foreach (var com in list)
            {
                if (com.transform.parent == x.transform.parent)
                {
                    return true;
                }
               /* if (GameCombine.CombineManager.Get<GameCombine.IItemCombineManager>().CheckIfConnected(name, this.gameObject.name))
                {
                    item.RotateRelativeToPointInWorld(this.transform.position, -5 * AngleSpeed);
                    return;

                }*/
            }
        }
        return false;
    }
    public static void GetAllConnectedCombinableItemInDfs(this CombinableItem item,ref List<CombinableItem>ans,CombinableItem lastItem=null)//static定义静态函数只能在声明他的文件中使用，好处在其他文件中定义不会冲突
    {

        for(int i= 0;i < 4; i++)
        {
            var nxt = GameCombine.CombineManager.Get<GameCombine.IItemCombineManager>().GetItemInJointDir(item.ItemFieldSetting.ItemName, i);
            if (string.IsNullOrEmpty(nxt) == false)
            {
                if(ans.Find(s=>s.ItemFieldSetting.ItemName==nxt)||(lastItem!=null&&lastItem.ItemFieldSetting.ItemName==nxt))
                {
                    continue;
                }
                else
                {
                    ans.Add(ClockManager.Get<IItemSyncManager>().GetGameObjectByName(nxt).GetComponent<CombinableItem>());
                    ans.Last<CombinableItem>().GetAllConnectedCombinableItemInDfs(ref ans,item);
                }
            }
        }
    }
}